package com.niit.project.filter;


import com.niit.project.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//过滤器的模糊匹配,这里表示过滤所有manager下的safe下的所有文件
@WebFilter("/manager/safe/*")
public class CheckFilter implements Filter {
    //初始化过滤器
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //将
        HttpServletRequest request = (HttpServletRequest) servletRequest;//将servletrequest强行转换为httpservletrequest
        HttpServletResponse response = (HttpServletResponse) servletResponse;//同上
        HttpSession session = request.getSession();//获取会话
        Admin admin = (Admin) session.getAttribute("empManager");//将会话信息存储在admin对象里
        if(admin!=null) //判断admin是否为空,如果不为空,则继续执行其他方法
        {
            filterChain.doFilter(request,response);
        }else {
            response.sendRedirect(request.getContextPath()+"/adminlogin.jsp");
        }


    }
    //销毁过滤器
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
